<?php
defined('IN_IA') or exit('Access Denied');


class Lorn_2048ModuleSite extends WeModuleSite {
    
    
   public $game = 'lorn_2048_games';
   public $user = 'lorn_2048_user';
   public $path = '../addons/lorn_2048/template';
   


   public function doMobileIndex()
   {            
        global $_GPC, $_W;    
        $gameid    = intval($_GPC['gid']);
        $game  = pdo_fetch("SELECT * FROM " . tablename($this->game) . " WHERE id = :id" ,array(':id'=>$gameid));
        $UrlPath = $this->path;
        include $this->template('index');
   }
   public function doMobileGame()
   {        
       
        global $_GPC, $_W;    
        $gameid    = intval($_GPC['gid']);
        if($gameid==''){
                message('此活动不存在！','','error');	
        }
        load()->model('mc');    
        $_W['fans'] =  mc_fansinfo($_W['fans']['openid']);
        if($_W['fans']['tag']['avatar']!='' && $_W['fans']['openid']
         &&$_W['fans']['tag']['nickname']!=''){
                $user['openid'] = $_W['fans']['openid'];
                $user['avatar'] = $_W['fans']['tag']['avatar'];
                $user['nickname'] = $_W['fans']['tag']['nickname'];
                $user['follow'] = $_W['fans']['follow'];
        }else{
                $fans = mc_oauth_userinfo();
                $user['avatar'] = $fans['headimgurl'];
                $user['openid'] = $fans['openid'];
                $user['nickname'] = $fans['nickname'];
                $user['follow'] = $fans['follow'];
        }    
        
        //限制访问
        if($user['openid']==''||$user['nickname']==''){
                message('请通过微信客户端登入','','error');	
        }
        $game_user = pdo_fetch("SELECT * FROM " . tablename($this->user) . " WHERE openid = :openid and gid = :gid and uniacid = :uniacid",array(':openid'=>$user['openid'],':gid'=>$gameid,':uniacid'=>$_W['uniacid']));
        
        if($game_user){
            $data['username'] = $user['nickname'];
            $data['avatar'] = $user['avatar'];
        
            pdo_update($this->user, $data, array(
                    'id' => $game_user['id']
            ));
        }else{
            $data['openid'] = $user['openid'];
            $data['gid'] = $gameid;
            $data['username'] = $user['nickname'];
            $data['avatar'] = $user['avatar'];
            $data['addtime'] = time();
            $data['uniacid'] = $_W['uniacid'];
            pdo_insert($this->user, $data);
            $game_user = pdo_fetch("SELECT * FROM " . tablename($this->user) . " WHERE openid = :openid and gid = :gid and uniacid = :uniacid",array(':openid'=>$user['openid'],':gid'=>$gameid,':uniacid'=>$_W['uniacid']));
        }
 
   
        $game  = pdo_fetch("SELECT * FROM " . tablename($this->game) . " WHERE id = " . $gameid);
        $text = explode("\n",$game['g_text']);
        foreach($text as &$row){
                $row = str_replace(array("\r\n", "\r", "\n"), "", $row); 
        }
        
        $UrlPath = $this->path;
        include $this->template('game');
   }
   public function doMobilePrize()
   {        
        global $_GPC, $_W;    
        $gameid    = intval($_GPC['gid']);
        $game  = pdo_fetch("SELECT * FROM " . tablename($this->game) . " WHERE id = :id" ,array(':id'=>$gameid));
        $UrlPath = $this->path;
        $prize = explode("\n",$game['prize']);
        foreach($prize as $v){
                $v = str_replace(array("\r\n", "\r", "\n"), "",$v); 
                $prizes[] = explode("|",$v);
        }
      
        include $this->template('prize');
   }
   public function doMobileBillboard()
   {        
        global $_GPC, $_W;    
        $gameid    = intval($_GPC['gid']);
      
        $pageNumber = max(1, intval($_GPC['page']));
        $pageSize   = 10;
        $sql   = "SELECT * FROM " . tablename($this->user) . "  where gid = ".$gameid." and uniacid =".$_W['uniacid']."  order by score desc limit 0,100";
        $sql2  = 'SELECT COUNT(*) FROM ' . tablename($this->user) . "  where gid = ".$gameid." and uniacid =".$_W['uniacid']." ORDER BY id DESC";
        $list  = pdo_fetchall($sql);
        $total = pdo_fetchcolumn($sql2);
        $pager = pagination($total, $pageNumber, $pageSize);
        load()->model('mc');    
        $_W['fans'] =  mc_fansinfo($_W['fans']['openid']);
        if($_W['fans']['tag']['avatar']!='' && $_W['fans']['openid']
         &&$_W['fans']['tag']['nickname']!=''){
                $user['openid'] = $_W['fans']['openid'];
                $user['avatar'] = $_W['fans']['tag']['avatar'];
                $user['nickname'] = $_W['fans']['tag']['nickname'];
                $user['follow'] = $_W['fans']['follow'];
        }else{
                $fans = mc_oauth_userinfo();
                $user['avatar'] = $fans['headimgurl'];
                $user['openid'] = $fans['openid'];
                $user['nickname'] = $fans['nickname'];
                $user['follow'] = $fans['follow'];
        }    
        
        if($user['openid']){
            foreach($list as $k=>$v){
                $uids[$v['openid']] = $v['score'] ;  
            }
            
            $uid = $uids[$user['openid']];
        }
        
        
        $UrlPath = $this->path;
        include $this->template('billboard');
   }
   public function doMobileDescribe()
   {        
        global $_GPC, $_W;    
        $gameid    = intval($_GPC['gid']);
        $game  = pdo_fetch("SELECT * FROM " . tablename($this->game) . " WHERE id = :id" ,array(':id'=>$gameid));
        $UrlPath = $this->path;
        include $this->template('describe');
   }
   
   public function doMobileJilu()
   {   
       
         
        global $_GPC, $_W;    
        $user_id = $_GPC['user_id'];
    	$gameid = $_GPC['gid'];        
    	$playtime = $_GPC['playtime'];
    	$score =  $_GPC['score'];
    	$game_result =  $_GPC['game_result'];
    	$last_time=  $_GPC['lasts_time'];
        load()->model('mc');    
        $_W['fans'] =  mc_fansinfo($_W['fans']['openid']);
        if($_W['fans']['tag']['avatar']!='' && $_W['fans']['openid']
         &&$_W['fans']['tag']['nickname']!=''){
                $user['openid'] = $_W['fans']['openid'];
                $user['avatar'] = $_W['fans']['tag']['avatar'];
                $user['nickname'] = $_W['fans']['tag']['nickname'];
                $user['follow'] = $_W['fans']['follow'];
        }else{
                $fans = mc_oauth_userinfo();
                $user['avatar'] = $fans['headimgurl'];
                $user['openid'] = $fans['openid'];
                $user['nickname'] = $fans['nickname'];
                $user['follow'] = $fans['follow'];
        }    
        //限制访问
        if($user['openid']==''||$user['nickname']==''){
                $res['status'] = 0;
                $res['msg'] = '请在微信内登录';
                echo json_encode($res);
                exit; 
        }
        
        
        $game_user = pdo_fetch("SELECT * FROM " . tablename($this->user) . " WHERE openid = :openid and gid = :gid and uniacid = :uniacid",array(':openid'=>$user['openid'],':gid'=>$gameid,':uniacid'=>$_W['uniacid']));
     
      
	if($user_id != $game_user['id']){
            $res['status'] = 0;
            $res['msg'] = '用户不匹配';
            echo json_encode($res);
            exit;
	}
        
        

	if($game_user['score'] < $score && $game_user){
		$data['score'] = $score;
		$data['res'] = $game_result;
		$data['playtime'] = $playtime;
		$data['last_time'] = $last_time;
        $data['uniacid'] = $_W['uniacid'];   
		 pdo_update($this->user, $data, array(
                        'id' => $game_user['id']
                ));
		$res['status'] = 1;
                $res['score'] = $score;
                $res['msg'] = '操作成功';
                echo json_encode($res);
        }else{
               $res['status'] = 0;
               $res['msg'] = '网络错误';
                echo json_encode($res);
        }
        exit;
   }
  public function dowebAddactivity()
    {
      
         global $_W, $_GPC;
         $gameid    = intval($_GPC['id']);
         if (checksubmit()) {       
             
            if(!$_GPC['title']){
                message('标题不能够为空！');
            }
            $data = array(
                'title' => $_GPC['title'],
                'g_text' => $_GPC['g_text'],
                'g_desc' => $_GPC['g_desc'],
                'prize' => $_GPC['prize'],
                'yxsm' => $_GPC['yxsm'],
                'p_shuoming' => $_GPC['p_shuoming'],
                'p_zanzhu' => $_GPC['p_zanzhu'],
                'bgpic' => $_GPC['bgpic'],
                'g_tpic' => $_GPC['g_tpic'],
                'hf_pic' => $_GPC['hf_pic'],
                'g_bpic' => $_GPC['g_bpic'],
                'phb_bg' => $_GPC['phb_bg'],
                'yxsm_pic' => $_GPC['yxsm_pic'],
                'prize_bg' => $_GPC['prize_bg'],
                'uniacid' => $_W['uniacid'],
                'addtime' => time()
            );
         
            if (!empty($gameid)) {
                pdo_update($this->game, $data, array(
                    'id' => $gameid
                ));
            } else {             
                pdo_insert($this->game, $data);
                $gameid = pdo_insertid();
            }
           
            message('操作成功！', referer(), 'success');
         }else{
           $game = pdo_fetch("SELECT * FROM " . tablename($this->game) . " WHERE  uniacid = ".$_W['uniacid']." and  id = :id", array(
                ':id' => $gameid
            ));
      
            include $this->template('addactivity'); 
         }       
    }
    public function dowebActivity()
    {
        global $_W, $_GPC;        
   
        $pageNumber = max(1, intval($_GPC['page']));
        $pageSize   = 10;
        $sql   = "SELECT * FROM " . tablename($this->game) . "  where uniacid = ".$_W['uniacid']." order by id desc";
        $sql2  = 'SELECT COUNT(*) FROM ' . tablename($this->game) . "  where uniacid = ".$_W['uniacid']." ORDER BY id DESC";
        $list  = pdo_fetchall($sql);
        $total = pdo_fetchcolumn($sql2);
        $pager = pagination($total, $pageNumber, $pageSize);
        include $this->template('activity');           
    }
    public function dowebDelact()
    {
       global $_W, $_GPC;   
       $gameid    = intval($_GPC['id']);
       $game  = pdo_fetch("SELECT id FROM " . tablename($this->game) . " WHERE  uniacid = ".$_W['uniacid']." and  id = :id" ,array(':id'=>$gameid));
      
        if (empty($game)) {
            message('抱歉，活动不存在或是已经被删除！');
        }
        pdo_delete($this->game, array(
            'id' => $gameid
        ));
        message('删除成功！', referer(), 'success');         
    }
    
    public function dowebRecord(){
        global $_W, $_GPC;        
        $keyword =  $_GPC['keyword'];
        $aid =  $_GPC['aid'];
        $pageNumber = max(1, intval($_GPC['page']));
        $pageSize   = 10;
        if($aid){
            $where = ' and gid = '.$aid;
        }
        if($keyword){
            $where .= " and username like '%".$keyword."%'";
        }
        $sql   = "SELECT * FROM " . tablename($this->user) . "  where uniacid =".$_W['uniacid']."  ".$where."     order by score,id desc";
        $sql2  = 'SELECT COUNT(*) FROM ' . tablename($this->user) . "  where uniacid =".$_W['uniacid']." ".$where."  ORDER BY score,id DESC";
        $list  = pdo_fetchall($sql);
        $total = pdo_fetchcolumn($sql2);
      
        $pager = pagination($total, $pageNumber, $pageSize);
        include $this->template('record');         
    }
    
    public function dowebDel_record(){
        global $_W, $_GPC;   
       $id    = intval($_GPC['id']);
       $record  = pdo_fetch("SELECT id FROM " . tablename($this->user) . " WHERE  uniacid = ".$_W['uniacid']." and  id = :id" ,array(':id'=>$id));
      
        if (empty($record)) {
            message('抱歉，记录不存在或是已经被删除！');
        }
        pdo_delete($this->user, array(
            'id' => $id
        ));
        message('删除成功！', referer(), 'success');        
    }
}